package com.aqumon.www.controller;

import com.alibaba.fastjson.JSONObject;
import com.aqumon.www.entity.request.jenkins.*;
import com.aqumon.www.entity.response.controller.Response;
import com.aqumon.www.service.JenkinsBuildService;
import com.aqumon.www.service.JenkinsConfigService;
import com.aqumon.www.service.ScriptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.net.URISyntaxException;

@RestController
@RequestMapping("/api/jenkins")
public class JenkinsController {
    @Resource
    JenkinsConfigService jenkinsConfigService;

    @Resource
    JenkinsBuildService jenkinsBuildService;


    private static final Logger LOGGER = LoggerFactory.getLogger(JenkinsController.class);

    /**
     * 保存jenkins 配置
     */
    @PostMapping(value = "/saveConfig")
    public Response<?>  saveConfig(@RequestBody ConfigCreateReq configCreateReq) throws IOException, URISyntaxException {
        configCreateReq.validate();
        LOGGER.info("保存jenkins 配置" + JSONObject.toJSONString(configCreateReq));
        return Response.success(jenkinsConfigService.saveConfig(configCreateReq));
    }

    /**
     * 编辑jenkins 配置
     */
    @PostMapping(value = "/editConfig")
    public Response<?>  editConfig(@RequestBody ConfigEditReq configEditReq) throws IOException, URISyntaxException {
        configEditReq.validate();
        LOGGER.info("修改jenkins 配置" + JSONObject.toJSONString(configEditReq));
        return Response.success(jenkinsConfigService.editConfig(configEditReq));
    }

    /**
     * 编辑jenkins 配置
     */
    @PostMapping(value = "/deleteConfig")
    public Response<?>  deleteConfig(@RequestBody ConfigDeleteReq deleteConfigReq) throws IOException, URISyntaxException {
        deleteConfigReq.validate();
        LOGGER.info("删除jenkins 配置id: " + JSONObject.toJSONString(deleteConfigReq));
        return Response.success(jenkinsConfigService.deleteConfig(deleteConfigReq));
    }
    /**
     * 获取所有jenkins 配置
     */
    @GetMapping(value = "/listAllConfig")
    public Response<?> listAllConfig() {
        LOGGER.info("获取所有jenkins 配置");
        return Response.success(jenkinsConfigService.listAllConfig());
    }

    /**
     * 获取指定job构建参数，无参数，返回为空
     */
    @GetMapping(value = "/getJobParams")
    public Response<?> getJobParams(@RequestParam String jenkinsName, String jobFullName) {
        LOGGER.info("查询job构建参数, jenkinsName:  " + jenkinsName + "jobFullName: " + jobFullName);
       return Response.success(jenkinsBuildService.getJobParams(jenkinsName, jobFullName));
    }

    /**
     * 开始构建
     */
    @PostMapping(value = "/build")
    public Response<?>  build(@RequestBody JobBuildReq jobBuildReq) throws IOException, URISyntaxException {
        LOGGER.info("调用构建job入参" + JSONObject.toJSONString(jobBuildReq));
        return Response.success(jenkinsBuildService.buildJob(jobBuildReq));
    }

    /**
     * 停止最近一次构建
     */
    @PostMapping(value = "/stopLastJobBuild")
    public Response<?>  stop(@RequestBody JobBasicReq jobBasicReq) {
        LOGGER.info("停止构建的job: " + JSONObject.toJSONString(jobBasicReq));
        return Response.success(jenkinsBuildService.stopLastJobBuild(jobBasicReq));
    }

    /**
     * 获取job指定buildNumber构建信息
     */
    @GetMapping(value = "/getBuildInfo")
    public Response<?> getLastBuildInfo(@RequestParam String jenkinsName,
                                        @RequestParam @NotNull(message = "jobName不能为空")  String jobFullName,
                                        @RequestParam(required = false)  Long buildNumber) {
        return Response.success(jenkinsBuildService.getBuildInfo(jenkinsName, jobFullName, buildNumber));
    }

    /**
     * 获取job指定buildNumber构建日志
    */
    @GetMapping(value = "/getBuildLog")
    public Response<?> getBuildLog(@RequestParam String jenkinsName,
                                   @RequestParam @NotNull(message = "jobName不能为空")  String jobFullName,
                                   @RequestParam(required = false)  Long buildNumber) {
        return Response.success(jenkinsBuildService.getBuildLog(jenkinsName, jobFullName, buildNumber));
    }

    /**
     * 停止job
     */
    @PostMapping(value = "/disableJob")
    public Response<?>  disableJob(@RequestBody JobBasicReq jobBasicReq) {
        LOGGER.info("停止job项目: " + JSONObject.toJSONString(jobBasicReq));
        return Response.success(jenkinsBuildService.disableJob(jobBasicReq));
    }

    /**
     * 启动job
     */
    @PostMapping(value = "/enableJob")
    public Response<?>  enableJob(@RequestBody JobBasicReq jobBasicReq) {
        LOGGER.info("开启job项目: " + JSONObject.toJSONString(jobBasicReq));
        return Response.success(jenkinsBuildService.enableJob(jobBasicReq));
    }

    /**
     * 获取指定脚本的上次构建
     */
    @GetMapping(value = "/getBuildList")
    public Response<?> getBuildList(@RequestParam Long scriptId) {
        LOGGER.info("获取指定脚本构建: " + scriptId.toString());
        return Response.success(jenkinsBuildService.getBuildList(scriptId));
    }
}
